<?php
/**
 * Created by PhpStorm.
 * User: mww
 * Date: 2018/12/17
 * Time: 16:08
 */
namespace app\api\controller;
use think\Db;
class Home extends Base
{
    /**
     * 异地登录验证
     * @return string|void
     */
    public function _initialize()
    {
        header('Access-Control-Allow-Origin:*');
        extract(input());
        if($user_id != 0){
            $auth = $this->checkAll($user_id,$time,$value,$reqKey);
            if($auth == 112){
                echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
                exit;
            }elseif($auth == 110){
                echo json_encode(['status' => 302, 'msg' => $this->frost]);
                exit;
            }elseif($auth == 1103){
                echo json_encode(['status' => 303, 'msg' => $this->del]);
                exit;
            }
        }

    }

    public function getMonthDays()
    {
        $month_date = $_POST['startmonth'];
        if(!$month_date){
            $month_date = date('Y-m');
        }
//        $month_date = '2020-08';
        $start_time = strtotime($month_date);
        $start_week = date('w', $start_time);
        $total_month_day = date('t', $start_time);
        $weeks_in_month = ceil(($start_week+$total_month_day)/7);
        $month_day_arr = [];
        $start_month_day = 1;
        $lastnum = 1;
        for($i=0;$i<$weeks_in_month;$i++) {
            for($j=0;$j<7;$j++){
                if($i ==0 && $j >= $start_week) {
                    $month_day_arr[$i][$j] = $month_date . '-' . sprintf("%02d",$start_month_day);
                    $start_month_day++;
                } elseif($i == 0) {
                    $jjj = $start_week - $j;
                    $month_day_arr[$i][$j] = date("Y-m-d",strtotime("$month_date -$jjj days"));
                } else {
                    $month_day_arr[$i][$j] = $month_date . '-' . sprintf("%02d",$start_month_day);
                    if($start_month_day > $total_month_day){
                        $strdate = $month_date . '-' . $total_month_day;
                        $month_day_arr[$i][$j] = date("Y-m-d",strtotime("$strdate +$lastnum days"));
                        $lastnum++;
                    }
                    $start_month_day++;
                }
            }
        }
//        dump($month_day_arr);die;
        $have_str = '';
        $have = Db::table('up_plan_practice')
            ->where('pra_student',$_POST['user_id'])
            ->where('pra_online',1)
            ->column('pra_date');
        if($have){
            $have_str = implode($have,',');
        }
        return json_encode(['status' => 200, 'msg' => $month_day_arr, 'have_str' => $have_str, 'today' => date('Y-m-d')]);
    }
    public function getDays7()
    {
        $month_date = $_POST['startdate'];
//        $month_date = '2020-07-11';
        if(!$month_date){
            $month_date = date('Y-m-d');
        }
        $prev = $month_date;
        $start_time = strtotime($month_date);
        $start_week = date('w', $start_time);
        $total_month_day = date('t', $start_time);
        $weeks_in_month = ceil(($start_week+$total_month_day)/7);
        $month_day_arr = [];
        $start_month_day = date("d",strtotime("$month_date"));
        $days = 0;
        for($i=0;$i<2;$i++) {
            for($j=0;$j<7;$j++){
                if($i ==0 && $j >= $start_week) {
                    $month_day_arr[$i][$j] = date("Y-m-d",strtotime("$month_date +$days days"));
                    $start_month_day++;
                    $days++;
                } elseif($i == 0) {
                    $jjj = $start_week - $j;
                    if($j == 0){
                        $prev = date("Y-m-d",strtotime("$month_date -$jjj days"));
                    }
                    $month_day_arr[$i][$j] = date("Y-m-d",strtotime("$month_date -$jjj days"));
                } else {
                    $month_day_arr[$i][$j] = date("Y-m-d",strtotime("$month_date +$days days"));
                    $start_month_day++;
                    $days++;
                }
            }
        }

        $now = $month_day_arr[0];
        $next = $month_day_arr[1];
        $prevarray = [];
        for($i=1;$i<=7;$i++) {
            $ji = 8-$i;
            $prevarray[$i-1] = date("Y-m-d",strtotime("$prev -$ji days"));
        }
//        dump($month_day_arr);dump($prevarray);die;
        $before = date("Y-m-d",strtotime("$prevarray[0] -1 day"));
        $after = date("Y-m-d",strtotime("$next[6] +1 day"));
        return json_encode([
            'status' => 200, 'msg' => $now, 'prev' => $prevarray, 'next' => $next, 'today' => date('Y-m-d')
            ,'before' => $before, 'after' => $after
        ]);
    }

    /**
     * 获取about表数据：
     * type：1 关于我们  2 客服电话  3 用户协议    4 隐私协议
     * @return string
     */
    public function getAbout()
    {
        extract(input());
        $res = Db::table('up_about')
            ->where('id',$type)
            ->find();
        return json_encode(['status'=>200,'msg'=>$res]);
    }
    /**
     * 获取首页数据：getHomeData
     *
     */
    public function getHomeData(){
        extract(input());
        $myda = Db::table('up_u_user')->where('user_id',$user_id)->field('user_piano,user_type')->find();
        $mypiano = $myda['user_piano'];
        $mytype = $myda['user_type'];
        if($mytype == 2){
            $lists = Db::table('up_list')
                ->where('l_status', 1)
                ->field(['l_id','l_title'])
                ->order('l_time DESC')
                ->page (1,3)
                ->select();
        }else{
            if(empty($mypiano)){
                $lists = Db::table('up_list')
                    ->where('l_type', 1)
                    ->where('l_student', $user_id)
                    ->where('l_status', 1)
                    ->field(['l_id','l_title'])
                    ->order('l_time DESC')
                    ->page (1,3)
                    ->select();
            }else{
                //1、大纲
                $lists = Db::table('up_list')
                    ->where(function ($query) use ($user_id) {
                        $query->where('l_type', 1)->where('l_student', $user_id)->where('l_status', 1);
                    })
                    ->whereOr(function ($query) use ($mypiano) {
                        $query->where('l_type', 2)->where('l_piano', 'like',"%$mypiano%")->where('l_status', 1);
                    })
                    ->field(['l_id','l_title'])
                    ->order('l_time DESC')
                    ->page (1,3)
                    ->select();
            }
        }


        //2、分类
        $axes = Db::table('up_axe')
            ->field('axe_id,axe_thumb,axe_name')
            ->select();
        //3、课程
        if($mytype == 2){
            $where = [
                'c_status' => 1
            ];
            if(isset($type) && $type){
                $where['c_axe'] = $type;
            }
            $courses = Db::table('up_course')->alias('c')
                ->join('up_u_user u','c.c_teacher=u.user_id','LEFT')
                ->where($where)
                ->field('c_id,c_name,c_thumb,c_num,user_id,user_username')
                ->order('c_time DESC')
                ->page (1,5)
                ->select();
        }else{
            $where = [
                'c_piano' => ['like',"%$mypiano%"],
                'c_status' => 1
            ];
            if(isset($type) && $type){
                $where['c_axe'] = $type;
            }
            if(empty($mypiano)){
                $courses = [];
            }else{
                $courses = Db::table('up_course')->alias('c')
                    ->join('up_u_user u','c.c_teacher=u.user_id','LEFT')
                    ->where($where)
                    ->field('c_id,c_name,c_thumb,c_num,user_id,user_username')
                    ->order('c_time DESC')
                    ->page (1,5)
                    ->select();
            }
        }
        Db::table('up_u_user')->where('user_id',$user_id)->setField('user_last_login_time',date('Y-m-d H:i:s'));
        return json(['status'=>200,'msg'=>$lists,'axes'=>$axes,'courses'=>$courses]);
    }


    /**
     * 查看更多大纲
     */
    public function getListMore()
    {
        extract(input());
        $myda = Db::table('up_u_user')->where('user_id',$user_id)->field('user_piano,user_type')->find();
        $mypiano = $myda['user_piano'];
        $mytype = $myda['user_type'];
        if($mytype == 2){
            $lists = Db::table('up_list')
                ->where('l_status', 1)
                ->field(['l_id','l_title'])
                ->order('l_time DESC')
                ->page ($page,15)
                ->select();
        }else {
            if (empty($mypiano)) {
                $lists = Db::table('up_list')
                    ->where('l_type', 1)
                    ->where('l_student', $user_id)
                    ->where('l_status', 1)
                    ->field(['l_id', 'l_title'])
                    ->order('l_time DESC')
                    ->page($page, 15)
                    ->select();
            } else {
                //1、大纲
                $lists = Db::table('up_list')
                    ->where(function ($query) use ($user_id) {
                        $query->where('l_type', 1)->where('l_student', $user_id)->where('l_status', 1);
                    })
                    ->whereOr(function ($query) use ($mypiano) {
                        $query->where('l_type', 2)->where('l_piano', 'like', "%$mypiano%")->where('l_status', 1);
                    })
                    ->field(['l_id', 'l_title'])
                    ->order('l_time DESC')
                    ->page($page, 15)
                    ->select();
            }
        }
        return json_encode(['status'=>200,'msg'=>$lists]);
    }
    /**
     * 查看大纲详情
     */
    public function getListInfo()
    {
        extract(input());
        $list = Db::table('up_list')
            ->where('l_id', $l_id)
            ->find();
        return json_encode(['status'=>200,'msg'=>$list]);
    }
    /**
     * 查看课程详情
     */
    public function getCourseInfo()
    {
        extract(input());
        $course = Db::table('up_course')->alias('c')
            ->join('up_u_user u','c.c_teacher=u.user_id','LEFT')
            ->where('c_id',$c_id)
            ->field('c_id,c_name,c_content,c_thumb,c_num,user_id,user_username,user_image,user_sign')
            ->order('c_time DESC')
            ->page (1,5)
            ->select();
        return json_encode(['status'=>200,'msg'=>$course]);
    }
    /**
     * 申请体验课
     * $c_id，$name，$phone
     */
    public function askCourseTest()
    {
        extract(input());
        $today = date('Y-m-d H:i:s');
        $cname = Db::table('up_course')->where('c_id',$c_id)->value('c_name');
        $data = [
            'ct_name' => $name,
            'ct_phone' => $phone,
            'ct_c_id' => $c_id,
            'ct_c_name' => $cname,
            'ct_uid' => $user_id,
            'ct_time' => $today,
            'ct_update_time' => $today
        ];
        $mypiano = Db::table('up_u_user')->where('user_id',$user_id)->value('user_piano');
        if(empty($mypiano)){
            $data['ct_p_name'] = '没有琴行';
            $data['ct_p_id'] = 0;
        }else{
            $pname = Db::table('up_piano')->where('pa_id',$mypiano)->value('pa_name');
            $data['ct_p_name'] = $pname;
            $data['ct_p_id'] = $mypiano;
        }
        $res = Db::table('up_course_test')->insert($data);
        if($res){
            Db::table('up_course')->where('c_id',$c_id)->setInc('c_num',1);
            return json_encode(['status'=>200,'msg'=>'提交成功']);
        }else{
            return json_encode(['status'=>0,'msg'=>'提交失败,请刷新重试']);
        }
    }

}